package com.run.cat.api.controller;


import com.run.cat.common.base.BaseResult;
import com.run.cat.common.util.EmptyUtil;
import com.run.cat.dao.entity.Attention;
import com.run.cat.dao.entity.Fond;
import com.run.cat.dao.entity.User;
import com.run.cat.service.IAttentionService;
import com.run.cat.service.IFondService;
import com.run.cat.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * <p>
 * 用户喜欢关系表 前端控制器
 * </p>
 *
 * @author CG
 * @since 2020-08-19
 */
@Controller
@RequestMapping("/fond")
public class FondController {

    @Autowired
    private IFondService fondService;

    @Autowired
    private IUserService userService;

    @RequestMapping("/add")
    @ResponseBody
    public BaseResult add(@RequestBody Fond fond){
        fondService.insert(fond);
        User fondUser = userService.selectById(fond.getFondUserId());
        if(EmptyUtil.isNotEmpty(fondUser)) {
            fondUser.setAdmire(fondUser.getAdmire() + 1);
            userService.updateById(fondUser);
        }
        return BaseResult.success();
    }


    @RequestMapping("/clean")
    @ResponseBody
    public BaseResult clean(@RequestBody Fond fond){
        User fondUser = userService.selectById(fond.getFondUserId());
        if(EmptyUtil.isNotEmpty(fondUser)) {
            fondUser.setAttention(fondUser.getAdmire()-1 > 0 ? 0:fondUser.getAdmire()-1);
            userService.updateById(fondUser);
        }
        fondService.deleteById(fond.getId());
        return BaseResult.success();
    }

}
